<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getLeaderboard, getMyRanking } from '@/services/api';

interface UserRanking {
  id: number;
  username: string;
  eloRating: number;
  wins: number;
  losses: number;
  ranking: number;
}

const myRanking = ref<UserRanking>({ id: 0, username: '', eloRating: 0, wins: 0, losses: 0, ranking: 0 });
const leaderboard = ref<UserRanking[]>([]);

const fetchLeaderboard = async () => {
  try {
    const [rankingRes, listRes] = await Promise.all([
      getMyRanking(),
      getLeaderboard(1, 10)
    ]);

    myRanking.value = rankingRes?.data?.data || { id: 0, username: '', eloRating: 0, wins: 0, losses: 0, ranking: 0 };
    leaderboard.value = listRes?.data?.data?.list || [];
  } catch (error) {
    console.error('获取排行榜失败:', error);
  }
};

onMounted(() => {
  fetchLeaderboard();
});
</script>

<template>
  <div class="leaderboard-section">
    <h3>当前排名：{{ myRanking.ranking }}位</h3>
    <div class="rank-list">
      <div v-for="(item, index) in leaderboard" :key="item.id" class="rank-item">
        <span class="rank-num">#{{ index + 1 }}</span>
        <span class="username">{{ item.username }}</span>
        <span class="elo">积分：{{ item.eloRating }}</span>
        <span class="wins">胜场：{{ item.wins }}</span>
        <span class="losses">败场：{{ item.losses }}</span>
      </div>
      <div v-if="!leaderboard || leaderboard.length === 0" class="loading">
        加载中...
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 容器优化 - 添加毛玻璃效果和动态阴影 */
.leaderboard-section {
  position: fixed;
  right: 20px;
  top: 20px;
  background: rgba(45,45,45,0.95);
  backdrop-filter: blur(12px) saturate(180%);
  border-radius: 16px;
  width: min(90vw, 380px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.2);
  border: 1px solid rgba(255,255,255,0.1);
  transition: transform 0.3s ease;
}

/* 标题样式增强 */
.leaderboard-section::before {
  content: "🏆 实时排行榜";
  display: block;
  font-size: 1.2rem;
  color: #FFD700;
  padding: 15px 20px;
  border-bottom: 1px solid rgba(255,215,0,0.2);
  background: linear-gradient(90deg, transparent 0%, rgba(255,215,0,0.1) 50%, transparent 100%);
}

/* 数据项现代网格布局 */
.rank-item {
  display: grid;
  grid-template-columns: 50px 1fr 80px 80px 80px;
  align-items: center;
  padding: 14px 20px;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

/* 动态悬浮效果 */
.rank-item:hover {
  background: rgba(255,255,255,0.03);
  transform: translateX(8px);
  box-shadow: inset 4px 0 0 rgba(0,229,255,0.6);
}

/* 名次徽章设计 */
.rank-num {
  font-weight: 700;
  font-size: 1.1em;
  position: relative;
  padding-left: 8px;
}

.rank-num::before {
  content: "";
  position: absolute;
  left: -8px;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 60%;
  background: linear-gradient(180deg, #FFB300 0%, #FF6B00 100%);
  border-radius: 2px;
}

/* 文字渐变效果 */
.username {
  color: #fff;
  font-weight: 500;
  position: relative;
  padding-left: 12px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.username::after {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 6px;
  height: 6px;
  background: #00E5FF;
  border-radius: 50%;
  opacity: 0.6;
}

/* 数据可视化增强 */
.elo, .wins, .losses {
  font-family: 'Roboto Mono', monospace;
  font-size: 0.95em;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.elo::before {
  content: "↑";
  color: #00C853;
  margin-right: 4px;
}

.wins::before {
  content: "✓";
  color: #00C853;
  margin-right: 4px;
}

.losses::before {
  content: "✕";
  color: #FF5252;
  margin-right: 4px;
}

/* 移动端适配 */
@media (max-width: 640px) {
  .leaderboard-section {
    right: 0;
    left: 0;
    top: auto;
    bottom: 0;
    width: 100%;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 32px rgba(0,0,0,0.2);
  }

  .rank-item {
    grid-template-columns: 40px 1fr 70px 70px 70px;
    padding: 12px 15px;
  }
}

/* 加载动画升级 */
.loading {
  padding: 20px;
  color: rgba(255,255,255,0.6);
  display: flex;
  justify-content: center;
  gap: 8px;
}

.loading::after {
  content: "";
  width: 1.2em;
  height: 1.2em;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #00E5FF;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
to { transform: rotate(360deg); }
}
</style>